import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/train/approve/tain_approve_main_page.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class TrainApproveWidget extends StatefulWidget {
  ///是否显示申请
  final bool isApploy;

  const TrainApproveWidget({Key key, this.isApploy = false}) : super(key: key);

  @override
  _TrainApproveWidgetState createState() => _TrainApproveWidgetState();
}

class _TrainApproveWidgetState extends State<TrainApproveWidget> {
  UserBean _userBean;

  @override
  void initState() {
    super.initState();
    _userBean = UserHelper.getInstance.userBean;
    int isApprove = _userBean.isApprove;
    if (isApprove != 0) {
      getApproveNumber();
    }
  }

  @override
  Widget build(BuildContext context) {
    int isApprove = _userBean.isApprove;
    if (isApprove == 0) {
      if (widget.isApploy) {
        return buildApployWidget();
      }
      return Container(
        height: 0,
      );
    }
    return Expanded(
      child: InkWell(
        onTap: () {
          NavigatorUtils.pushPage(context, TrainApproveMainPage(),
              callback: (value) {
            getApproveNumber();
          });
        },
        child: buildContainer(context),
      ),
    );
  }

  Container buildContainer(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 12, right: 12, top: 8, bottom: 8),
      padding: EdgeInsets.only(left: 12, right: 12),
      height: 54,
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(8))),
      width: MediaQuery.of(context).size.width,
      child: Row(
        children: [
          Stack(
            alignment: Alignment.centerLeft,
            children: [
              Image.asset(
                "assets/images/2.0x/home_train_approve.gif",
              ),
              buildNumber(),
            ],
          ),
          SizedBox(
            width: 10,
          ),
          Expanded(
            child: Text(
              "审批中心",
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 16,
                color: Colors.blueGrey,
              ),
            ),
          ),
          SizedBox(
            width: 10,
          ),
        ],
      ),
    );
  }

  int _number = 0;

  buildNumber() {
    if (_number == 0) {
      return Container();
    } else {
      return ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(12)),
        child: Container(
          alignment: Alignment.center,
          constraints: BoxConstraints(minWidth: 20, maxHeight: 20),
          padding: EdgeInsets.all(2),
          color: Colors.red,
          child: Text(
            "$_number",
            style: TextStyle(
              fontSize: 12,
              color: Colors.white,
            ),
          ),
        ),
      );
    }
  }

  Future<bool> getApproveNumber() async {
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.trainApproveNumber,
    );

    if (responseInfo.success) {
      _number = responseInfo.data;
    }

    if (!mounted) return true;
    setState(() {});
    return Future.value(true);
  }

  ///显示一个申请按钮
  Widget buildApployWidget() {}
}
